<?php
/*Session variables:
 * $_SESSION['userId'], the user id of the user accessing the system
 * $_SESSION['companyId'], the company id of the user accessing the system
 * $_SESSION['dbSchema'], The name of the user database schema in which the accounting system resides, to get its value call function getDbSchema, 
 * it is the same as company code
 * $_SESSION['language'] The company language so that reports will be displayed in this language
 * $_SESSION['branches'], contains the user branches 
 */ 

/**
 * Get a list of accounts for last level (level 6) and this is a detailed account, by default firs branch
   @author Ghassan Al-Noubani
*/
function getAccountList($selectedBranch ,$fromAccount = null) 
{
    $query = "
        SELECT 
            GL_CHR_ACCNO, GL_CHR_NAME
        FROM 
            GLCHRTBL
        WHERE "; 
        
        if( '00' !== $selectedBranch) {   
            $query .= " CS_BRN_CODE = '".$selectedBranch."' AND ";
        }
        
       $query .= "
        CS_BRN_CODE IN ".$_SESSION['branches'] . " AND
        GL_CHR_LEVEL = '6' AND
        GL_CHR_DETAIL = '2' ";
        
        if (null !== $fromAccount) {
            $query .= " AND GL_CHR_ACCNO >= ".$fromAccount;       
        }
        $query .= "
            ORDER BY
                GL_CHR_ACCNO";
}
/**
    @author Ghassan Al-Noubani
*/
function GLR24CHR($selectedBranch , $fromAccount, $toAccount, $fromDate, $toDate)
{
    $query = "
        SELECT 
            GL_CHR_ACCNO, CS_BRN_CODE, CS_TRX_CODE, GL_TRX_VOCHNO, TRUNC(GL_TRX_DATE), 
            GL_TRX_NOTE, NVL(GL_TRX_DBVAL,0),NVL(GL_TRX_CRVAL,0)
        FROM 
            GLTRXTBL
        WHERE 
            GL_CHR_ACCNO BETWEEN ".$fromAccount." AND ".$toAccount." 
           AND TRUNC(GL_TRX_DATE) BETWEEN TO_DATE(".$fromDate." ,'DD/MM/YYYY')AND TO_DATE(".$toDate." ,'DD/MM/YYYY') AND";
        if( '00' !== $selectedBranch) {   
            $query .= " CS_BRN_CODE = '".$selectedBranch."' AND ";
        }
        $query .= "
           CS_BRN_CODE IN ".$_SESSION['branches'] . " AND
           GL_TRX_PSTFLG = '1' AND
           GL_CHR_ACCNO IN (
                SELECT 
                    GL_CHR_ACCNO 
                FROM 
                    GLCHRTBL 
                WHERE 
                    CS_CHR_CODE IN (SELECT CS_CHR_CODE FROM CSUHRCOD WHERE CS_USR_CODE=:PUSR)) AND //Account type
           CS_BRN_CODE  IN (SELECT CS_BRN_CODE  FROM CSUBRCOD WHERE CS_USR_CODE = :PUSR) //Branch
           ORDER BY GL_TRX_DATE, CS_BRN_CODE, CS_TRX_CODE, GL_TRX_VOCHNO"
}



?>
